/*
 * Created on 12-Jul-06 Filename: SubmitCommand.java @author Daniel Yule
 */
package com.danicsoft.daide.command;

import java.util.List;

import com.danicsoft.daide.token.TokenGroup;
import com.danicsoft.daide.token.TokenMap.Commands;

/**
 * Implements the SUB command. From Eric Wald's <a
 * href="http://pydip.brainshell.org/docs/syntax.html">protocol specification</a>,
 * v0.14:
 * <h4><a name="SUB">player_request = SUB</a> (order) (order) ...</h4>
 * <p>
 * This is sent from the player to the server, to submit orders for the next
 * turn. <b>order</b> can take the following form:
 * </p>
 * <ul>
 * <li>Movement phase orders:
 * <ul>
 * <li><b>order = (unit) HLD</b> &mdash; Hold</li>
 * <li><b>order = (unit) MTO province</b> &mdash; Move</li>
 * <li><b>order = (unit) SUP (unit)</b> &mdash; Support to hold</li>
 * <li><b>order = (unit) SUP (unit) MTO prov_no_coast</b> &mdash; Support to
 * move</li>
 * <li><b>order = (unit) CVY (unit) CTO province</b> &mdash; Convoy</li>
 * <li><b>order = (unit) CTO province VIA (sea_province sea_province ...)</b>
 * &mdash; Move by convoy</li>
 * </ul>
 * </li>
 * <li>Retreat phase orders:
 * <ul>
 * <li><b>order = (unit) RTO province</b> &mdash; Retreat to</li>
 * <li><b>order = (unit) DSB</b> &mdash; Disband unit</li>
 * </ul>
 * </li>
 * <li>Build phase orders:
 * <ul>
 * <li><b>order = (unit) BLD</b> &mdash; Build unit</li>
 * <li><b>order = (unit) REM</b> &mdash; Remove unit</li>
 * <li><b>order = power WVE</b> &mdash; Waive build</li>
 * </ul>
 * </li>
 * </ul>
 * <p>
 * <b>unit</b> is a unit, and must be in the form as given by <a
 * href="#NOW">the <b>NOW</b> command</a> &mdash; that is, <b>(power type
 * province)</b> &mdash; for example, <b>(ENG FLT LON)</b>.
 * </p>
 * <p>
 * <b>province</b> is any province. For a fleet moving to a province with
 * multiple coasts (Spain, St. Petersburg, or Bulgaria on the Standard map), it
 * must be of the form <b>(province coast)</b>.
 * </p>
 * <p>
 * <b>prov_no_coast</b> is a province with no coast specification.
 * </p>
 * <p>
 * <b>sea_province</b> is a province where a fleet may be to convoy.
 * </p>
 * <p>
 * Examples:
 * </p>
 * <ul>
 * <li><b>SUB ((ENG AMY LVP) HLD) ((ENG FLT LON) MTO NTH) ((ENG FLT EDI) SUP
 * (ENG FLT LON) MTO NTH)</b></li>
 * <li><b>SUB ((ENG FLT BAR) MTO (STP NCS)) ((ENG FLT NWY) SUP (ENG FLT BAR)
 * MTO STP)</b></li>
 * <li><b>SUB ((ITA AMY TUN) CTO SYR VIA (ION EMS)) ((ITA FLT ION) CVY (ITA AMY
 * TUN) CTO SYR) ((ITA FLT EMS) CVY (ITA AMY TUN) CTO SYR)</b></li>
 * <li><b>SUB ((ENG AMY YOR) CTO NWY VIA (NTH)) ((ENG FLT NTH) CVY (ENG AMY
 * YOR) CTO NWY) ((ENG FLT NWG) SUP (ENG AMY YOR) MTO NWY)</b></li>
 * <li><b>SUB ((ITA FLT NAP) BLD)</b></li>
 * <li><b>SUB ((RUS FLT GOB) REM)</b></li>
 * </ul>
 * <p>
 * All unused builds must be waived, even if they are unusable.
 * </p>
 * <p>
 * The server will reply to each order (not each message), in left-to-right
 * order through the orders, with a <b>THX</b> message. This is of the format:
 * </p>
 * 
 * @author Daniel Yule (daniel.yule@gmail.com)
 * @version 1.0
 */
public class SubmitCommand extends QueryableCommand {

	/**
	 * The orders we are submitting.
	 */
	private List<OrderSpec> orders;

	/**
	 * Construct a new <code>SubmitCommand</code> from the tokens given.
	 * 
	 * @param tokens
	 *        A <code>TokenGroup</code> which contains a list of orders
	 * @throws ParseException
	 *         If there was a problem parsing the tokens
	 */
	public SubmitCommand(TokenGroup tokens) throws ParseException {
		super(tokens);
	}

	/**
	 * Construct a new <code>SubmitCommand</code> for sending to the server as part of a NOT
	 * command.
	 */
	public SubmitCommand() {}

	/**
	 * Construct a new <code>SubmitCommand</code> for sending to the server.
	 * 
	 * @param orders
	 *        A list of orders to submit.
	 */
	public SubmitCommand(List<OrderSpec> orders) {
		super(false);
		this.orders = orders;

	}

	@Override
	public byte getType() {
		return Commands.SUB;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("Submitting:\n");
		for (OrderSpec ord : orders) {
			sb.append('\t');
			sb.append(ord.toString());
			sb.append('\n');
		}
		return sb.toString();
	}

	/**
	 * Gets the orders that this command is submitting
	 * 
	 * @return A list of orders.
	 */
	public List<OrderSpec> getOrders() {
		return orders;
	}

	@Override
	protected void doNonQueryIO(TokenIO tokenIO, TokenGroup tokens) throws ParseException {
		orders = tokenIO.doList(tokens, orders, new OrderSpec());

	}

}
